<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="处理状态">
        <el-select v-model="queryParams.processingStatus" placeholder="请选择" clearable>
          <el-option label="待处理" value="待处理"></el-option>
          <el-option label="处理中" value="处理中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="订单状态">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择" clearable>
          <el-option label="全部" value=""></el-option>
          <el-option label="待审核" value="待审核"></el-option>
          <el-option label="待调度" value="待调度"></el-option>
          <el-option label="运输中" value="运输中"></el-option>
          <el-option label="已签收" value="已签收"></el-option>
          <el-option label="已拒绝" value="已拒绝"></el-option>
          <el-option label="已取消" value="已取消"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="事件时间">
        <el-date-picker
          v-model="daterangeEventTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="处理时间">
        <el-date-picker
          v-model="daterangeProcessingTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['monitoring:exmanagement:add']"
        >上传</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['monitoring:exmanagement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['monitoring:exmanagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['monitoring:exmanagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="exmanagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="异常单号" align="center" prop="waybillNumber" width="130">
        <template slot-scope="scope">
          <el-link @click="handleUpdate2(scope.row)" type="primary">{{scope.row.exceptionNumber}}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="客户" align="center" prop="customer" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="承运商" align="center" prop="carrier" />
      <el-table-column label="司机" align="center" prop="driver" />
      <el-table-column label="手机号码" align="center" prop="phoneNumber" />
      <el-table-column label="车牌号" align="center" prop="licensePlate" />
      <el-table-column label="事件类型" align="center" prop="eventType" />
      <el-table-column label="事件时间" align="center" prop="eventTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.eventTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="事件地点" align="center" prop="eventLocation" />
      <el-table-column label="事件说明" align="center" prop="eventDescription" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="上报人" align="center" prop="reporter" />
      <el-table-column label="上报时间" align="center" prop="reportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="processingStatus" />
      <el-table-column label="处理人" align="center" prop="processor" />
      <el-table-column label="处理时间" align="center" prop="processingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.processingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理措施" align="center" prop="measuresTaken" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['monitoring:exmanagement:edit']"
          >修改</el-button>

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.processingStatus=='待处理'"
            @click="handleUpdate2(scope.row)">处理异常</el-button>

          <el-button
            size="mini"
            type="text"
            v-if="scope.row.processingStatus=='处理中'"
            @click="updateStatus(scope.row.id)"
          >完成</el-button>



          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['monitoring:exmanagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改异常管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="事件类型">
          <el-select v-model="form.eventType" placeholder="请选择" clearable>
            <el-option label="交通事故" value="交通事故"></el-option>
            <el-option label="车辆故障" value="车辆故障"></el-option>
            <el-option label="货物破损" value="货物破损"></el-option>
            <el-option label="地址错误" value="地址错误"></el-option>
            <el-option label="收件人拒收" value="收件人拒收"></el-option>
            <el-option label="丢失包裹" value="丢失包裹"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="运单号" prop="waybillNumber">
          <el-input v-model="form.waybillNumber" placeholder="请输入运单号" />
        </el-form-item>
<!--        <el-form-item label="异常单号" prop="exceptionNumber">-->
<!--          <el-input v-model="form.exceptionNumber" placeholder="请输入异常单号" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="客户" prop="customer">-->
<!--          <el-input v-model="form.customer" placeholder="请输入客户" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="承运商" prop="carrier">-->
<!--          <el-input v-model="form.carrier" placeholder="请输入承运商" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="司机" prop="driver">-->
<!--          <el-input v-model="form.driver" placeholder="请输入司机" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="手机号码" prop="phoneNumber">-->
<!--          <el-input v-model="form.phoneNumber" placeholder="请输入手机号码" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="车牌号" prop="licensePlate">-->
<!--          <el-input v-model="form.licensePlate" placeholder="请输入车牌号" />-->
<!--        </el-form-item>-->

        <el-form-item label="图片上传" >
          <el-upload
            :on-success="onUploadSuccess"
            :before-upload="beforeUpload"
            :on-preview="onUploadPreview"
            :on-remove="onUploadRemove"
            :multiple="true"
            :action="BASE_API+'/pricture/fileUpload'"
            class="upload-demo"
            v-model="form.image"
            list-type="picture-card"
            :file-list="fileList">
            <i class="el-icon-plus"/>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png/gif文件，且不超过2MB</div>
          </el-upload>
        </el-form-item>


        <el-form-item label="事件时间" prop="eventTime">
          <el-date-picker clearable
            v-model="form.eventTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择事件时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="事件地点" prop="eventLocation">
          <el-input v-model="form.eventLocation" placeholder="请输入事件地点" />
        </el-form-item>
<!--        <el-form-item label="图片" prop="image">-->
<!--          <image-upload v-model="form.image"/>-->
<!--        </el-form-item>-->



        <el-form-item label="事件说明" prop="eventDescription">
          <el-input v-model="form.eventDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>

<!--        <el-form-item label="上报人" prop="reporter">-->
<!--          <el-input v-model="form.reporter" placeholder="请输入上报人" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="上报时间" prop="reportTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.reportTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择上报时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="处理人" prop="processor">-->
<!--          <el-input v-model="form.processor" placeholder="请输入处理人" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="处理时间" prop="processingTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.processingTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择处理时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="处理措施" prop="measuresTaken">-->
<!--          <el-input v-model="form.measuresTaken" type="textarea" placeholder="请输入内容" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExmanagement, getExmanagement, delExmanagement, addExmanagement, updateExmanagement } from "@/api/monitoring/exmanagement";

export default {
  name: "Exmanagement",
  data() {
    return {
      // 接口API地址
      BASE_API: 'http://localhost:8901',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 异常管理表格数据
      exmanagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 处理措施时间范围
      daterangeEventTime: [],
      // 处理措施时间范围
      daterangeProcessingTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderStatus: null,
        eventTime: null,
        processingStatus: null,
        processingTime: null,
      },
      // 表单参数
      form: {},
      fileList: [],
      exmPosterList:[],
      exmImagesList:[],
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    if(this.$route.query.processingStatus != null && this.$route.query.processingStatus != undefined){
      this.queryParams.processingStatus = this.$route.query.processingStatus;
      this.getList();
    }
    this.getList();
  },
  methods: {




    /** 查询异常管理列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeEventTime && '' != this.daterangeEventTime) {
        this.queryParams.params["beginEventTime"] = this.daterangeEventTime[0];
        this.queryParams.params["endEventTime"] = this.daterangeEventTime[1];
      }
      if (null != this.daterangeProcessingTime && '' != this.daterangeProcessingTime) {
        this.queryParams.params["beginProcessingTime"] = this.daterangeProcessingTime[0];
        this.queryParams.params["endProcessingTime"] = this.daterangeProcessingTime[1];
      }
      listExmanagement(this.queryParams).then(response => {
        this.exmanagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        waybillNumber: null,
        exceptionNumber: null,
        customer: null,
        orderStatus: null,
        carrier: null,
        driver: null,
        phoneNumber: null,
        licensePlate: null,
        eventType: null,
        eventTime: null,
        eventLocation: null,
        eventDescription: null,
        image: null,
        reporter: null,
        reportTime: null,
        processingStatus: null,
        processor: null,
        processingTime: null,
        measuresTaken: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeEventTime = [];
      this.daterangeProcessingTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加异常管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getExmanagement(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改异常管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate2(row) {
      // this.reset();
      const id = row.id || this.ids
      this.$router.push({path:'/operation/operationXq',query:{id:id}})
      // getOperation(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改运单";
      // });
    },


    xiang(id){
      this.$router.push({path:'/exxq',query:{id:id}})
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExmanagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addExmanagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 状态修改按钮操作*/
    updateStatus(id){
      this.axios.put(`http://localhost:8901/exmanagement/updateStatus?id=${id}`).then(res=>{
        if(res.data.code===1){
          this.$message.success("修改成功")
        }else{
          this.$message.error("修改失败")
        }
        this.getList();
      })
    },
    // 删除上传的文件
    onUploadRemove(file, fileList) {
      var list = []
      this.exmImagesList.forEach(item => {
        if(file.url != item.imgUrl) {
          list.push(item)
        }
      })
      this.exmImagesList = list
    },
    // 上传图片成功的回调
    onUploadSuccess(res, file) {
      console.log(res,file);
      this.form.image = res.data;
      // 填充上传文件列表
      this.exmImagesList.push({
        imgName: file.name,
        imgUrl: file.response.data
      })
    },
    // 文件上传限制条件
    beforeUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isGIF = file.type === 'image/gif'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG && !isPNG && !isGIF) {
        this.$message.error('上传头像图片只能是 JPG、PNG 或 GIF 格式!')
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
        return false
      }
      return true
    },
    // 上传的文件预览
    onUploadPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogImageVisible = true
    },// 删除上传的文件
    onUploadPosterRemove(file, fileList) {
      var list = []
      this.skuInfo.empPosterList.forEach(item => {
        if(file.url != item.imgUrl) {
          list.push(item)
        }
      })
      this.skuInfo.empPosterList = list
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除异常管理编号为"' + ids + '"的数据项？').then(function() {
        return delExmanagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('monitoring/exmanagement/export', {
        ...this.queryParams
      }, `exmanagement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
